﻿import QtQuick 2.11
//import QtQuick.Controls 1.4
import QtGraphicalEffects 1.0

import com.leamus.gamedata 1.0

import "../"
import "../_Global"
import "../Poke"

import "../_Global/Global.js" as GlobalJS
import "../GameJS.js" as GameJS

//import "../_Global/Global.js" as GlobalJS
//import "../Poke/Temp.js" as PokeClass

GameItem {
    property bool bDead: true
    property int nShouPaiCount: 0

    property alias source: imagePlayer.source
    property alias sourceSize: imagePlayer.sourceSize

    //根据Player的listJiNeng来初始化(!!弃用)
    function initJiNeng() {
        var count = GameCore.playerList[root.vUserData].jiNengList.length;
        //console.debug("initJiNeng", root.vUserData, count)

        //遍历JiNeng进行初始化
        var i;
        for(i = 0; i < count; i++)
        {
            if(_private.arrayJiNeng.length <= i) { //如果不够则创建
                var jn = _private.tmpBindJiNeng(i);
                jn.s_clicked.connect(function(clicked,userData){  //绑定信号
                    GameCore.sl_qml_JiNengClicked(clicked, userData, root.vUserData);
                });
                _private.arrayJiNeng.push(jn);
            }
            _private.arrayJiNeng[i].pokeSource = _private.getJiNengImage(GameCore.playerList[root.vUserData].jiNengList[i].value);
            _private.arrayJiNeng[i].visible = true;
        }

        //剩下的隐藏
        for(; i < _private.arrayJiNeng.length; i++)
        {
            _private.arrayJiNeng[i].pokeSource = ""
            _private.arrayJiNeng[i].visible = false;
        }
    }

    //设置血量
    function setBlood(blood, maxBlood) {
        if(maxBlood !== undefined && maxBlood >= 0)
            _private.nBloodMaxCount = maxBlood;
        if(blood !== undefined && blood >= 0)
            _private.nBloodCount = blood;

        _private.refreshBloodShow();
    }

    //刷新血量blood,若是小于当前血,则有 掉血动画效果
    function refreshBlood(blood) {
        if(_private.nBloodCount > blood) {
            aniSprite.playSprite();
            shake();
            if(Global.config.bEffectOn)
                Global.resource.effects.effectHarm.play();
        }
        _private.nBloodCount = blood;
        _private.refreshBloodShow();
    }


    //visible: nFlags & PokeClass.flag_Show

    id: root
    bEnable: (nFlags & PokeClass.Flag_Enable)           //是否可点
    bClicked: (nFlags & PokeClass.Flag_Clicked)          //点击状态
    //width: playerBack.width
    //height: playerBack.height

    //武将背景
    Rectangle {
        id: playerBack
        //source: Global._FixResourcePath_R("Media/System/photo-back.png")
        width: root.width
        height: root.height
        color: "#00000000"


        //边框效果
        RectangularGlow {
            id: rectGlow
            visible: root.bEnable && (root.bClicked)// || mouseArea.containsMouse)
            anchors.fill: parent
            glowRadius: 10  //边框宽度
            spread: 0.2    //虚化程度(0~1)
            color: root.bClicked ? "red" : "blue"  //颜色

            //z: -1
            cornerRadius: parent.radius + glowRadius
        }

        //Rectangle { //用来遮罩
        //    id: imagePlayerRect
            //width: Global.dpW(145)  //119*60
            //height: Global.dpH(71)
            //武将图片
            Image {
                id: imagePlayer
                anchors.fill: parent
                x: Global.dpW(7)        //6,26
                y: Global.dpH(31)
            }
        //}
        //颜色遮罩(不可用时黑色)
        ColorOverlay {
            id: colorOverlay
            visible: bDead
            //visible: !root.bEnable
            anchors.fill: imagePlayer
            source: imagePlayer
            color: "#80000000"
        }



        AnimatedSprite {
            id: aniSprite
            anchors.fill: parent
            //width: 132
            //height: 130
            source: Global._FixResourcePath_R("Media/Images/Game/Animates/Killed.png")
            frameCount: 7
            frameSync: false
            frameWidth: 132
            frameHeight: 130
            loops: 1
            running: false
            //frameRate
            frameDuration: 150
            interpolate: true       //平滑
            visible: running

            function playSprite() {
                //aniSprite.stop();
                aniSprite.currentFrame = 0;
                aniSprite.start();
            }

            /*
            onCurrentFrameChanged: {
                if(currentFrame == frameCount - 1)      //注意:因为播放完最后一帧以后,会很快的跳转到第一帧后才消失!!!
                    running = false;
            }
            */
        }



        MouseArea {
            id: mouseArea
            //enabled: root.bEnable
            hoverEnabled: true
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            //propagateComposedEvents: true //当为true时，可以传递鼠标事件。默认值为false

            //hoverEnabled: true
            /*
            onPressed: {
            }
            */
            onClicked: {
                if(bEnable) {
                    //bClicked = !bClicked
                    //nFlags ^= PokeClass.flag_Clicked

                    /*
                    console.debug(GameCore.playerList[vUserData].flags)
                    console.debug(GameCore.playerList[vUserData])
                    console.debug(GameCore.playerList[vUserData].id)
                    console.debug(bEnable,bClicked,vUserData)
                    */
                    root.s_clicked(bClicked,vUserData);
                }
                else {
                    mouse.accepted = true;
                }
            }

            //onEntered: console.debug(rectGlow.visible)
        }


    }

    //手牌数
    Text {
        id: textShouPaiCount
        x: Global.gamePos.mapPlayer.posPlayerPokeCount.x
        y: Global.gamePos.mapPlayer.posPlayerPokeCount.y
        //width: Global.gamePos.mapPlayer.posPlayerBlood.x
        //height: Global.gamePos.mapPlayer.posPlayerBlood.x
        z: 100
        text: nShouPaiCount

        font {
            //family: Global.resource.fonts.font1.name
            pixelSize: Global.gamePos.mapPlayer.nPlayerPokeCountFontSize
            bold: true
        }
        color: "gray"
        //style: Text.Outline
        //styleColor: "lightgreen"
    }


    //单个血量图片
    Component {
        id: bloodComp
        Image {
            x: Global.gamePos.mapPlayer.posPlayerBlood.x
            //y: Global.gamePos.mapPlayer.posPlayerBlood.y
            width: Global.gamePos.mapPlayer.sizePlayerBlood.width
            height: Global.gamePos.mapPlayer.sizePlayerBlood.height
        }
    }

    //血量图片缓存
    Component {
        id: bloodCacheComp
        Image {
            anchors.fill: parent
            visible: false
        }
    }


/*
    function setPlayer(src) {
        imagePlayer.source = src
    }
*/

    QtObject {
        id: _private

        property int nBloodCount: 0          //目前血量
        property int nBloodMaxCount: 0       //血量上限

        property var arrayBloodImage: new Array //缓存血量的图片
        property var arrayBlood: new Array //血量数组(4个)

        property var arrayJiNeng: new Array //技能数组(n个)

        readonly property int nSHOWBLOODMAXCOUNT: 4
        //刷新显示
        /*
        function refreshBloodShow() {   //我的方式,一个颜色4滴
            for(var i = 0; i < 5; i++) {
                if(i < nBloodMaxCount) {         //是否显示此血槽
                    arrayBlood[i].visible = true;
                    if(nBloodCount > 5 && (i < nBloodCount % 5 || nBloodCount / 5 >= 2 ))  //超过5血,显示绿色
                        arrayBlood[i].source = arrayBloodImage[2].source;
                    else if(i < nBloodCount)     //5血以下,红色
                        arrayBlood[i].source = arrayBloodImage[1].source;
                    else        //空位为黑色
                        arrayBlood[i].source = arrayBloodImage[0].source;
                }
                else
                    arrayBlood[i].visible = false;
            }
        }*/

        function refreshBloodShow() {
            var i, n;
            n = (nBloodMaxCount > nSHOWBLOODMAXCOUNT ? nSHOWBLOODMAXCOUNT : nBloodMaxCount);
            for(i = 0; i < n; i++) {        //遍历4个血槽
                arrayBlood[i].visible = true;
                if(i < nBloodCount) {
                    switch(nBloodCount) {
                    case 1:
                        arrayBlood[i].source = arrayBloodImage[1].source;
                        break;
                    case 2:
                        arrayBlood[i].source = arrayBloodImage[2].source;
                        break;
                    case 3:
                        arrayBlood[i].source = arrayBloodImage[3].source;
                        break;
                    case 4:
                        arrayBlood[i].source = arrayBloodImage[4].source;
                        break;
                    case 5:
                        arrayBlood[i].source = arrayBloodImage[1].source;
                        break;
                    case 6:
                        arrayBlood[i].source = arrayBloodImage[2].source;
                        break;
                    case 7:
                        arrayBlood[i].source = arrayBloodImage[3].source;
                        break;
                    }
                }
                else
                    arrayBlood[i].source = arrayBloodImage[0].source;
            }
            for(; i < _private.nSHOWBLOODMAXCOUNT; i++) {
                arrayBlood[i].visible = false;
            }
        }

        function tmpBindBloodY(i,tmpObj) {
            tmpObj.y = Qt.binding(function() {
                return Global.gamePos.mapPlayer.posPlayerBlood.y - i * Global.gamePos.mapPlayer.nPanDingYSpacing
                // Global.dpH(127 - i * 20)
            })
        }

        function tmpBindJiNeng(i) {
            return compJiNeng.createObject(root, {
                                        "x": Qt.binding(function(){return Global.gamePos.mapPlayer.posPlayerJiNeng.x + i * Global.gamePos.mapPlayer.nJiNengXSpacing;})
                                        ,"y": Qt.binding(function(){return Global.gamePos.mapPlayer.posPlayerJiNeng.y;})
                                        ,"width": Qt.binding(function(){return Global.gamePos.mapPlayer.sizePlayerJiNeng.width;})
                                        ,"height": Qt.binding(function(){return Global.gamePos.mapPlayer.sizePlayerJiNeng.height;})
                                        ,"vUserData": i
                                    });
        }

        //onNBloodCountChanged: refreshBloodShow()
        //onNBloodMaxCountChanged: refreshBloodShow()
    }



    Component.onCompleted: {
        //for(var i = 0; i < 2; i++) {
        //imageArray[i] = Qt.createQmlObject('import QtQuick 2.2; Image {anchors.fill: parent}',root);
        //    _private.arrayBloodImage[i] = bloodCacheComp.createObject(root,{"source": "Blood/0.png"})
        //}
        //载入图片资源
        _private.arrayBloodImage[0] = bloodCacheComp.createObject(root,{"source": Global._FixResourcePath_R("Media/Images/Game/Bloods/0.png")})
        _private.arrayBloodImage[1] = bloodCacheComp.createObject(root,{"source": Global._FixResourcePath_R("Media/Images/Game/Bloods/1.png")})
        _private.arrayBloodImage[2] = bloodCacheComp.createObject(root,{"source": Global._FixResourcePath_R("Media/Images/Game/Bloods/2.png")})
        _private.arrayBloodImage[3] = bloodCacheComp.createObject(root,{"source": Global._FixResourcePath_R("Media/Images/Game/Bloods/3.png")})
        _private.arrayBloodImage[4] = bloodCacheComp.createObject(root,{"source": Global._FixResourcePath_R("Media/Images/Game/Bloods/4.png")})
        //imageArray[0].visible = true


        //角色的4个血块
        for(var i = 0; i < _private.nSHOWBLOODMAXCOUNT; i++) {
            _private.arrayBlood[i] = bloodComp.createObject(playerBack,{
                                                                //"x": Qt.binding(function(){return Global.dpW(105 - i * 18)})
                                                            })
            _private.tmpBindBloodY(i,_private.arrayBlood[i]);  //绑定 y
        }
        //setPlayer("erzhang.png")
        //refreshBlood(10,10)
        //root.enabled = false;
    }
}


